package com.yylending.las.lending.quota.credit;

import com.yyfax.common.CLog;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yylending.las.Global;
import com.yylending.las.common.AESEntity;

import java.io.Serializable;

import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

/**
 * Created by laixj on 2016/11/18.
 * Copyright © 2016 www.yyfax.com. All Rights Reserved.
 */
public class CreditModel extends Model {

	private static final String TAG = "CreditModel";

	public final static int METHOD_GET_VERIFY_INFO = 0;
	public final static int METHOD_SAVE_AUTH_CODE = 1;

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		switch (functionalCode) {
			case METHOD_GET_VERIFY_INFO:{
				Service service = (Service) NetworkHelper.createService(Service.class, 120);
				return service.getVerifyInfo(Global.LOGIN_TOKEN, (AESEntity) params);}
			case METHOD_SAVE_AUTH_CODE:{
				Service service = (Service) NetworkHelper.createService(Service.class, 240);
				return service.saveVerifyCode(Global.LOGIN_TOKEN, (AESEntity) params);}
		}
		return null;
	}

	@Override
	public void onNext(Object obj) {
		CLog.i(TAG, "onNext: " + obj.toString());
		BaseResultEntity result = cast(obj);
		switch (result.requestCode) {
			case METHOD_GET_VERIFY_INFO:
				VerifyInfoResult verifyInfoResult = cast(obj);
				EventAgent.post(verifyInfoResult);
				break;
			case METHOD_SAVE_AUTH_CODE:
				SaveVerifyCodeResult saveVerifyCodeResult = cast(obj);
				EventAgent.post(saveVerifyCodeResult);
				break;
		}
	}

	private interface Service {
		/**
		 * 请求征信查询报文接口
		 */
		@POST("server/customer/getVerifyInfo")
		Observable<VerifyInfoResult> getVerifyInfo(@Header("token") String token, @Body AESEntity params);
		/**
		 * 上传人行征信验证码接口
		 */
		@POST("server/customer/checkVerifyCode")
		Observable<SaveVerifyCodeResult> saveVerifyCode(@Header("token") String token, @Body AESEntity params);
	}

	static class VerifyInfoResult extends BaseResultEntity<VerifyInfo> {
	}

	static class SaveVerifyCodeResult extends BaseResultEntity<CheckVerifyCode> {
	}

	static class CheckVerifyCode{
		public String amountState;
		public String userName;
	}

	public static class SaveVerifyCode {
		/**
		 * 6位征信授权码
		 */
		public String verifyCode;
		/**
		 * 经度
		 */
		public String longitude;
		/**
		 * 纬度
		 */
		public String latitude;

		SaveVerifyCode(String verifyCode, String longitude, String latitude) {
			this.verifyCode = verifyCode;
			this.longitude = longitude;
			this.latitude = latitude;
		}
	}

	public static class GetVerifyInfo {
		/**
		 * 认证请求来源 0:pc 1:app
		 */
		public String sourceType = "0";
		/**
		 * 银联授权商户返回地址
		 */
		public String metUrl;

		GetVerifyInfo(String sourceType, String metUrl) {
			this.sourceType = sourceType;
			this.metUrl = metUrl;
		}
	}

	static class VerifyInfo implements Serializable{
		/**
		 * 人行认证请求编码
		 */
		public String charset;
		/**
		 * 人行认证机构代码
		 */
		public String certOrgCode;
		/**
		 * 人行认证地址
		 */
		public String authUrl;
		/**
		 * 人行认证加密信息
		 */
		public String encrypMessage;
		/**
		 * 人行认证签名信息
		 */
		public String signMessage;
		/**
		 * 人行认证交易流水号
		 */
		public String serialNumber;
		/**
		 * 请求流水号，提交认证返回的code需要
		 */
		public String verifyId;
		/**
		 * 人行认证版本号
		 */
		public String version;
		/**
		 * 人行认证加密方法
		 */
		public String encrypMethod;
		/**
		 * 人行认证签名方法
		 */
		public String signMethod;
	}
}
